// CustomContextPad.js
import store from "../../../store/index"
export default class CustomContextPad {
    constructor(config, contextPad, create, elementFactory, injector, translate, modeling) {
        this.create = create;
        this.elementFactory = elementFactory;
        this.translate = translate;
        this.modeling = modeling
        if (config.autoPlace !== false) {
            this.autoPlace = injector.get('autoPlace', false);
        }

        contextPad.registerProvider(this); // 定义这是一个contextPad
    }

    getContextPadEntries(element) {
        const { autoPlace, create, elementFactory, translate, modeling } = this;

        // 删除节点
        function deleteElement() {
            return {
                group: 'edit',
                className: 'icon-custom icon-custom-delete',
                title: translate('删除'),
                action: {
                    click: function () {
                        modeling.removeElements([element])
                    }
                }
            }
        }
        // 编辑节点
        function editElement() { // 创建编辑图标
            return {
                group: 'edit',
                className: 'icon-custom icon-custom-edit',
                title: translate('编辑'),
                action: {
                    click: function () {
                        store.commit("bpmn_store/setNodeInfo", element)
                        store.commit("bpmn_store/toggeleNodeInfo")
                    }
                }
            }
        }

        function appendTask(event, element) {
            if (autoPlace) {
                const shape = elementFactory.createShape({ type: 'bpmn:Task' });
                autoPlace.append(element, shape);
            } else {
                appendTaskStart(event, element);
            }
        }

        function appendTaskStart(event) {
            const shape = elementFactory.createShape({ type: 'bpmn:Task' });
            create.start(event, shape, element);
        }


        if (element.type === "bpmn:StartEvent") {
            return {
                'edit': editElement(), // 返回值加上编辑功能
            };
        } else {
            return {
                'append.lindaidai-task': {
                    group: 'model',
                    className: 'icon-custom lindaidai-task',
                    title: translate('创建一个类型为lindaidai-task的任务节点'),
                    action: {
                        click: appendTask,
                        dragstart: appendTaskStart
                    }
                },
                'delete': deleteElement(), // 返回值加上删除的功能
                'edit': editElement(), // 返回值加上编辑功能
            };
        }


    }
}

CustomContextPad.$inject = [
    'config',
    'contextPad',
    'create',
    'elementFactory',
    'injector',
    'translate',
    'modeling'
];